package dongshi.daddy.facade.workuse.v2;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

public class MessageSenderFactory {
    private static Map<Integer, MessageSender> productMap = new HashMap<>();

    static {
        ServiceLoader<MessageSender> load = ServiceLoader.load(MessageSender.class);
        Iterator<MessageSender> iterator = load.iterator();
        while (iterator.hasNext()) {
            MessageSender next = iterator.next();
            Class<? extends MessageSender> aClass = next.getClass();
            if (!aClass.isAnnotationPresent(Key.class)) throw new IllegalStateException("class: " + aClass + " expect @com.jh.framework.page.annotation.Key, but not found!");
            int id = aClass.getAnnotation(Key.class).id();
            productMap.put(id, next);
        }
    }

    public static MessageSender makeProduct(int code) {
        return productMap.get(code);
    }

}
